using System.Collections;
using Holoville.HOTween;
using Holoville.HOTween.Core;
using UnityEngine;

public class MarketPreviewController : MonoBehaviour
{
	public UIWidget widget;

	public RotationObject rotator;

	public Transform carPreviewPoint;

	public PlayerInShopManager playerPreview;

	public EquipmentManager playerArmorPreview;

	public GameObject shopPreviewObject;

	public static VehicleUpgradesPreviewer upgradePreviewer;

	private Product defaultWeapon;

	private Product currentWeapon;

	private Product currentArmor;

	private Product currentHat;

	private Product currentBoot;

	private Coroutine _coroutineSetNewCar;

	private string loadedCarID;

	private GameObject currentCar;

	private ResourceRequest resourceRequest;

	private Vector3 _initialCarPreviewPosition = Vector3.zero;

	private Tweener SlideOutTweener;

	private bool slideOutCarComplete = true;

	private GameObject currentObject;

	public Product DefaultWeapon
	{
		get
		{
			if (defaultWeapon == null)
			{
				defaultWeapon = MarketController.Instance.GetRandomEquipedWeaponProduct();
			}
			return defaultWeapon;
		}
		set
		{
			defaultWeapon = value;
		}
	}

	private Vector3 initialCarPreviewPosition
	{
		get
		{
			if (_initialCarPreviewPosition == Vector3.zero)
			{
				_initialCarPreviewPosition = carPreviewPoint.localPosition;
			}
			return _initialCarPreviewPosition;
		}
	}

	private void Awake()
	{
		HOTween.Init();
	}

	public void ShowPreview()
	{
		playerPreview.ChangeSkin(string.Empty);
		switch (MarketControllerUI.instance.selectedProduct.GetType().ToString())
		{
		case "ProductWeapon":
			ShowArmorPreview(MarketController.Instance.GetEquipedProduct("Armor", "Body"));
			ShowHatPreview(MarketController.Instance.GetEquipedProduct("Armor", "Hats"));
			ShowBootPreview(MarketController.Instance.GetEquipedProduct("Armor", "Boots"));
			ShowWeaponPreview(MarketControllerUI.instance.selectedProduct);
			break;
		case "ProductComsumable":
			ShowArmorPreview(MarketController.Instance.GetEquipedProduct("Armor", "Body"));
			ShowHatPreview(MarketController.Instance.GetEquipedProduct("Armor", "Hats"));
			ShowBootPreview(MarketController.Instance.GetEquipedProduct("Armor", "Boots"));
			ShowWeaponPreview(MarketControllerUI.instance.selectedProduct);
			break;
		case "ProductCar":
		case "ProductTank":
		case "ProductTitan":
			ShowCarPreview(MarketControllerUI.instance.selectedProduct);
			break;
		case "ProductArmor":
			ShowWeaponPreview(DefaultWeapon);
			ShowHatPreview(MarketController.Instance.GetEquipedProduct("Armor", "Hats"));
			ShowBootPreview(MarketController.Instance.GetEquipedProduct("Armor", "Boots"));
			ShowArmorPreview(MarketControllerUI.instance.selectedProduct);
			break;
		case "ProductSkin":
			ShowArmorPreview(null);
			ShowWeaponPreview(null);
			ShowHatPreview(null);
			ShowBootPreview(null);
			ShowSkinPreview(MarketControllerUI.instance.selectedProduct);
			break;
		case "ProductHat":
			ShowHatPreview(MarketControllerUI.instance.selectedProduct);
			ShowBootPreview(MarketController.Instance.GetEquipedProduct("Armor", "Boots"));
			break;
		case "ProductBoot":
			ShowBootPreview(MarketControllerUI.instance.selectedProduct);
			ShowHatPreview(MarketController.Instance.GetEquipedProduct("Armor", "Hats"));
			break;
		case "ProductBoost":
			Show3DObject(MarketControllerUI.instance.selectedProduct);
			break;
		}
	}

	public void ShowWeaponPreview(Product product)
	{
		rotator.usedAnimation = false;
		rotator.rotateObject = playerPreview.gameObject;
		playerPreview.gameObject.SetActive(true);
		carPreviewPoint.gameObject.SetActive(false);
		shopPreviewObject.SetActive(false);
		if (currentWeapon != product)
		{
			if (product == null)
			{
				playerPreview.ShowWeapon(null);
				currentWeapon = null;
			}
			else
			{
				playerPreview.ShowWeapon(product.id);
				currentWeapon = product;
			}
		}
	}

	public void ShowSkinPreview(Product product)
	{
		rotator.usedAnimation = false;
		rotator.rotateObject = playerPreview.gameObject;
		playerPreview.gameObject.SetActive(true);
		carPreviewPoint.gameObject.SetActive(false);
		playerPreview.ChangeSkin(product.id);
	}

	public void ShowArmorPreview(Product product)
	{
		rotator.usedAnimation = false;
		rotator.rotateObject = playerPreview.gameObject;
		playerPreview.gameObject.SetActive(true);
		carPreviewPoint.gameObject.SetActive(false);
		shopPreviewObject.SetActive(false);
		if (currentArmor != product)
		{
			if (product == null)
			{
				playerArmorPreview.PutOnArmor(null);
				currentArmor = null;
			}
			else
			{
				EquipmentManager.LoadAndEquipArmor(product.id, playerArmorPreview, false);
				currentArmor = product;
			}
		}
	}

	public void ShowHatPreview(Product product)
	{
		rotator.usedAnimation = false;
		rotator.rotateObject = playerPreview.gameObject;
		playerPreview.gameObject.SetActive(true);
		carPreviewPoint.gameObject.SetActive(false);
		shopPreviewObject.SetActive(false);
		if (currentHat != product)
		{
			if (product == null)
			{
				playerArmorPreview.PutOnHat(null);
				currentHat = null;
			}
			else
			{
				EquipmentManager.LoadAndEquipHat(product.id, playerArmorPreview, false);
				currentHat = product;
			}
		}
	}

	public void ShowBootPreview(Product product)
	{
		rotator.usedAnimation = false;
		rotator.rotateObject = playerPreview.gameObject;
		playerPreview.gameObject.SetActive(true);
		carPreviewPoint.gameObject.SetActive(false);
		shopPreviewObject.SetActive(false);
		if (currentBoot != product)
		{
			if (product == null)
			{
				playerArmorPreview.PutOnBoots(null, null);
				currentBoot = null;
			}
			else
			{
				EquipmentManager.LoadAndEquipBoots(product.id, playerArmorPreview, false);
				currentBoot = product;
			}
		}
	}

	public void ShowCarPreview(Product product)
	{
		rotator.usedAnimation = true;
		rotator.rotateObject = carPreviewPoint.gameObject;
		playerPreview.gameObject.SetActive(false);
		carPreviewPoint.gameObject.SetActive(true);
		shopPreviewObject.SetActive(false);
		if (_coroutineSetNewCar != null)
		{
			StopCoroutine(_coroutineSetNewCar);
			_coroutineSetNewCar = null;
		}
		_coroutineSetNewCar = StartCoroutine(SetNewCar(product.id));
	}

	private IEnumerator SetNewCar(string id)
	{
		if ((bool)currentCar)
		{
			SlideOutCar();
		}
		resourceRequest = Resources.LoadAsync("ShopCars/" + id);
		yield return resourceRequest;
		while (!slideOutCarComplete)
		{
			yield return null;
		}
		if ((bool)currentCar)
		{
			Object.Destroy(currentCar);
		}
		currentCar = Object.Instantiate(resourceRequest.asset) as GameObject;
		currentCar.transform.SetParent(carPreviewPoint);
		currentCar.transform.localPosition = Vector3.zero;
		currentCar.transform.localScale = Vector3.one * 0.5f;
		currentCar.transform.localRotation = Quaternion.Euler(0f, 145f, 0f);
		loadedCarID = id;
		upgradePreviewer = currentCar.GetComponent<VehicleUpgradesPreviewer>();
		if (upgradePreviewer != null)
		{
			ProductVehicleSkin vehSkin = MarketController.Instance.GetEquipedVehicleSkin(id);
			if (vehSkin != null)
			{
				upgradePreviewer.ShowSkin(Resources.Load<Texture>("AllCarSkins/" + vehSkin.skinTextureName));
			}
			else
			{
				upgradePreviewer.ShowDefault();
			}
		}
		Vector3 newPosion = initialCarPreviewPosition;
		newPosion.x += 10f;
		carPreviewPoint.localPosition = newPosion;
		rotator.rotateObject = carPreviewPoint.gameObject;
		rotator.usedAnimation = false;
		rotator.ResetRotation();
		HOTween.To(carPreviewPoint, 0.25f, new TweenParms().Prop("localPosition", initialCarPreviewPosition).UpdateType(UpdateType.TimeScaleIndependentUpdate).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			rotator.usedAnimation = true;
		}));
	}

	private void SlideOutCar()
	{
		slideOutCarComplete = false;
		HOTween.Kill(SlideOutTweener);
		Vector3 vector = initialCarPreviewPosition;
		vector.x += 10f;
		SlideOutTweener = HOTween.To(carPreviewPoint, 0.25f, new TweenParms().Prop("localPosition", vector).UpdateType(UpdateType.TimeScaleIndependentUpdate).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			slideOutCarComplete = true;
		}));
	}

	private void Show3DObject(Product product)
	{
		playerPreview.gameObject.SetActive(false);
		carPreviewPoint.gameObject.SetActive(false);
		shopPreviewObject.SetActive(true);
		if (!(loadedCarID == product.id))
		{
			CoroutineRunner.Instance.StartCoroutine(SetNewObject(product.id));
		}
	}

	private IEnumerator SetNewObject(string id)
	{
		resourceRequest = Resources.LoadAsync("ShopGuns/" + id);
		yield return resourceRequest;
		if ((bool)currentObject)
		{
			Object.Destroy(currentObject);
		}
		currentObject = Object.Instantiate(resourceRequest.asset) as GameObject;
		currentObject.transform.SetParent(shopPreviewObject.transform);
		currentObject.transform.localPosition = Vector3.zero;
		currentObject.transform.localScale = Vector3.one * 1.5f;
		currentObject.transform.localRotation = Quaternion.Euler(0f, 145f, 0f);
		Vector3 newPosion = initialCarPreviewPosition;
		newPosion.x += 10f;
		carPreviewPoint.localPosition = newPosion;
		rotator.rotateObject = shopPreviewObject;
		rotator.usedAnimation = true;
		rotator.ResetRotation();
	}
}
